//------------------------------------------------------------------------------
// <copyright company="Telligent Systems">
//     Copyright (c) Telligent Systems Corporation.  All rights reserved.
// </copyright> 
//------------------------------------------------------------------------------

using System.Xml;
using CommunityServer.Components;

namespace CommunityServer.Galleries.Components
{

	/// <summary>
	/// Summary description for WeblogEmailNotificationModule.
	/// </summary>
	public class GalleryEmailNotificationModule : ICSModule
	{
		public GalleryEmailNotificationModule()
		{
		}

		public void Init(CSApplication csa, XmlNode node)
		{
			csa.PostPostUpdate +=new CSPostEventHandler(csa_PostPostUpdate);
		}

		private void csa_PostPostUpdate(IContent content, CSPostEventArgs e)
		{
			GalleryPost p = content as GalleryPost;
			if(p != null && e.State != ObjectState.Delete)
			{

				// Retrieve the parent if this is feedback. Do it here so we don't need 2 DB hits
				GalleryPost parent = null;
				if (!p.IsPicture)
					parent = GalleryPosts.GetPicture(p.ParentID);

					if (e.State == ObjectState.Create)
					{
				// Send email notification to Gallery Owner depending on FeedbackNotificationType setting
				switch (p.FeedbackNotificationType)
				{
					case FeedbackNotificationType.AllFeedback:
					{
						if (!p.IsPicture)
							GalleryEmails.GalleryFeedbackNotification(p, parent);

						break;
					}

					case FeedbackNotificationType.AllComments:
					{
						if (p.GalleryPostType == GalleryPostType.Comment)
							GalleryEmails.GalleryFeedbackNotification(p, parent);

						break;
					}
				
				}
					}
				
				// Process email subscriptions only if post is approved
				if (p.IsApproved)
					GalleryEmails.GalleryTracking(p, parent);

			}

		}


	}

}
